#!/bin/bash
#
# Copyright © 2024 Broadcom. All rights reserved.
#
# The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. This software
# and all information contained therein is confidential and proprietary and shall not
# be duplicated, used, disclosed or disseminated in any way except as authorized by
# the applicable license agreement, without the express written permission of Broadcom.
# All authorized reproductions must be marked with this language.
#
# EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT PERMITTED BY
# APPLICABLE LAW OR AS AGREED BY BROADCOM IN ITS APPLICABLE LICENSE AGREEMENT, BROADCOM
# PROVIDES THIS DOCUMENTATION “AS IS” WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT
# LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
# OR NONINFRINGEMENT. IN NO EVENT WILL BROADCOM BE LIABLE TO THE END USER OR ANY THIRD
# PARTY FOR ANY LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS DOCUMENTATION,
# INCLUDING WITHOUT LIMITATION, LOST PROFITS, LOST INVESTMENT, BUSINESS INTERRUPTION,
# GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS EXPRESSLY ADVISED IN ADVANCE OF
# THE POSSIBILITY OF SUCH LOSS OR DAMAGE.
#

# test
#function SYM_SILO_NAME()
#{
# echo "MES"
#}

INSTALLER_RESULT=$1

SCRIPTDIR=`/usr/bin/dirname "$0"`

cd "$SCRIPTDIR"

PARTNER_FILE=./com.symantec.partner.plist
PARTNER_DEST="/Library/Application Support/Symantec/Registry"
readonly BROADCOM_APP_UPGRADE_FLAG="/private/tmp/.sep.broadcom.upgrade.flag"
readonly TEMPSYLINKPATH="/private/tmp/SyLink.xml"
readonly TEMPSETUPINI="/private/tmp/setup.ini"
readonly TEMPSETAIDINI="/private/tmp/setAid.ini"
readonly TEMPSERDEF="/private/tmp/Serdef.dat"
readonly PARTNER_TMP_FILE_PATH="/private/tmp/com.symantec.partner.plist"

function clean_up()
{
  /bin/rm -rf "${SCRIPTDIR}"
  /bin/rm -f "${BROADCOM_APP_UPGRADE_FLAG}"
  /bin/rm -f "${TEMPSYLINKPATH}"
  /bin/rm -f "${TEMPSETUPINI}"
  /bin/rm -f "${TEMPSETAIDINI}"
  /bin/rm -f "${TEMPSERDEF}"
  /bin/rm -f "${PARTNER_TMP_FILE_PATH}"
}

# if product was installed properly, then process partner and environment variant files
if [ $INSTALLER_RESULT -eq 0 ]; then
  echo "install succeed, continue sympostinstall"
else
  echo "install failed, just do clean up"
  clean_up
  exit 0
fi

function launch_uiagent()
{
  loggedInUser=""
  loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
  echo "$loggedInUser is login"
  if [ ! -z "$loggedInUser" ]; then
    echo su - "$loggedInUser" -c "launchctl load /Library/LaunchAgents/com.symantec.uiagent.application.MES.plist"
    su - "$loggedInUser" -c "launchctl load /Library/LaunchAgents/com.symantec.uiagent.application.MES.plist"
  fi
}

readonly TARGET_MODE_FLAG_FOLDER="/private/tmp/"
readonly TARGET_FLAG_UPGRADE_NLOK="${TARGET_MODE_FLAG_FOLDER}/ses_mac.upgrade.nlok.flag"
readonly SEP_APP="/Applications/Symantec Endpoint Protection.app/Contents/MacOS/Symantec Endpoint Protection"
if [[ -f "$SEP_APP" ]]; then
  if [[ -f "$TARGET_FLAG_UPGRADE_NLOK" ]]; then
    # upgrade nlok, old nlok daemons running
    # activate system extension will lost network
    echo "upgrade nlok, skip activate system extension"
  else
    loggedInUser=""
    loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
    echo "$loggedInUser is login"
    if [ ! -z "$loggedInUser" ]; then
      #add sleep to void null network profile name
      sleep 5
      echo su - "$loggedInUser" -c "$SEP_APP -activateSystemExtension"
      su - "$loggedInUser" -c "'$SEP_APP' -activateSystemExtension"
      # if not upgrade PKG installer will trigger load network profile
      if [ ! -f "${BROADCOM_APP_UPGRADE_FLAG}" ]; then
        sleep 5
        echo su - "$loggedInUser" -c "$SEP_APP -loadNetworkProfile"
        SEP_INSTALLATION_LOG="/tmp/sep.mac.loadNetworkProfile.log"
        su - "$loggedInUser" -c "echo '$(date '+%Y-%m-%d %H:%M:%S'): start to loadNetworkProfile' >> '${SEP_INSTALLATION_LOG}' 2>&1"
        su - "$loggedInUser" -c "'$SEP_APP' -loadNetworkProfile >> '${SEP_INSTALLATION_LOG}' 2>&1 &"
        # wait loadNetworkProfile output ready
        sleep 1
      fi
    fi
  fi
fi

if [[ -f "$TARGET_FLAG_UPGRADE_NLOK" ]]; then
  rm -f "$TARGET_FLAG_UPGRADE_NLOK"
  echo "remotepostinstall upgrade nlok, skip launch uiagent"
else
  launch_uiagent
fi
clean_up

exit 0


